import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Pointer Event'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  //定义一个状态，保存当前指针位置
  PointerEvent _event1;
  PointerEvent _event2;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: new Column(
          children: <Widget>[
            new Listener(
              child: new Container(
                alignment: Alignment.center,
                color: Colors.blue,
                constraints: new BoxConstraints(
                  minWidth: double.infinity,
                  minHeight: 200,
                  maxHeight: 200,
                ),
                child: new Text(
                  _event1?.position.toString() ?? '',
                  style: TextStyle(color: Colors.white, fontSize: 15),
                ),
              ),
              //behavior: HitTestBehavior.opaque,
              onPointerDown: (PointerDownEvent event) {
                setState(() {
                  _event1 = event;
                });
              },
              onPointerMove: (PointerMoveEvent event) {
                setState(() {
                  _event1 = event;
                });
              },
              onPointerUp: (PointerUpEvent event) {
                setState(() {
                  _event1 = event;
                });
              },
            ),
            new Padding(
              padding: EdgeInsets.only(top: 20),
              child: new Listener(
                child: new AbsorbPointer(
                  child: new Listener(
                    //点击Container时，由于它在AbsorbPointer的子树上，所以不会响应指针事件
                    child: new Container(
                      alignment: Alignment.center,
                      color: Colors.red,
                      width: double.infinity,
                      height: 200,
                      child: new Text(_event2?.position.toString() ?? '',
                          style: TextStyle(color: Colors.white, fontSize: 15)),
                    ),
                    onPointerDown: (PointerDownEvent event) {
                      print('哈哈哈哈');
                    },
                    onPointerMove: (PointerMoveEvent event) {
                      print('呵呵呵呵');
                    },
                  ),
                ),
                //behavior: HitTestBehavior.opaque,
                onPointerDown: (PointerDownEvent event) {
                  setState(() {
                    _event2 = event;
                  });
                },
                onPointerMove: (PointerMoveEvent event) {
                  setState(() {
                    _event2 = event;
                  });
                },
                onPointerUp: (PointerUpEvent event) {
                  setState(() {
                    _event2 = event;
                  });
                },
              ),
            )
          ],
        ));
  }
}
